package cn.edu.sdju.wanglo1911.team10.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.sdju.wanglo1911.team10.po.OrderItem;
import cn.edu.sdju.wanglo1911.team10.po.OrderSummary;
import cn.edu.sdju.wanglo1911.team10.po.QueryOption;
import cn.edu.sdju.wanglo1911.team10.po.UserAccount;
import cn.edu.sdju.wanglo1911.team10.service.OrderService;
import cn.edu.sdju.wanglo1911.team10.service.Sha256Service;
import cn.edu.sdju.wanglo1911.team10.service.UserAccountService;

@Controller
public class UserController {
	
	@Autowired
	private UserAccountService userAccountService;
	@Autowired
	private Sha256Service sha256Service;
	@Autowired
	private OrderService orderService;
	
	@RequestMapping(value = "/login",method = {RequestMethod.GET, RequestMethod.POST})
	public String login(HttpServletRequest request,String nick,String password, String redirect,HttpSession session,Model model) {
		if ("GET".equals(request.getMethod())) {
			model.addAttribute("loginTitle", "用户登录");
			model.addAttribute("redirect", redirect);
			model.addAttribute("helpMessage", "然而并没有帮助👻");
			return "user_login";
		}
		UserAccount account = userAccountService.getUserAccountByNick(nick);
		if (account != null && sha256Service.digest(password).equals(account.getPassword())) {
			session.setAttribute("account", account);
			if (redirect == null || redirect.equals("")) {
				redirect = "/";
			}
			return "redirect:" + redirect;
		}
		model.addAttribute("loginTitle", "用户登录");
		model.addAttribute("redirect", redirect);
		model.addAttribute("helpMessage", "然而并没有帮助👻");
		model.addAttribute("error", "用户名或密码错误");
		return "user_login";
	}
	
	@GetMapping("profile")
	public String profile(HttpServletRequest request, HttpSession session, Model model) {
		UserAccount account = (UserAccount) session.getAttribute("account");
		QueryOption option = new QueryOption();
		option.setQueryType("userId");
		option.setQuery(account.getId());
		List<OrderSummary> orderSummaries = orderService.advanceQuery(option);
		model.addAttribute("account", account);
		model.addAttribute("orderSummaries", orderSummaries);
		return "user_profile";
	}
	
	@GetMapping("/profile/order/{orderNo}")
	public String userViewOrder(HttpServletRequest request,@PathVariable String orderNo,HttpSession session, Model model) {
		UserAccount account = (UserAccount) session.getAttribute("account");
		List<OrderItem> orderItems = orderService.getAllItemByOrderNo(orderNo);
		model.addAttribute("account", account);
		model.addAttribute("orderItems", orderItems);
		return "user_profile_view_order";
	}
	
	
	@GetMapping("register")
	public String registerGet() {
		return "user_register";
	}
	@PostMapping("register")
	public String registerPost(HttpServletRequest request, UserAccount account, HttpSession session, Model model) {
		account.setId(UUID.randomUUID().toString().substring(0, 32));
		account.setPassword(sha256Service.digest(account.getPassword()));
		userAccountService.crateUserAccount(account);
		model.addAttribute("message", "注册成功！");
		return "message";
	}
	
	@RequestMapping(value = "logout",method = {RequestMethod.GET, RequestMethod.POST})
	public String logout(HttpServletRequest request,Model model) {
		HttpSession session = request.getSession();
		if (session != null) {
			session.removeAttribute("account");
			model.addAttribute("msg", "ok");
		}
		return "redirect:/";
	}
	
}
